<?php
/**
 * Main Jobs SpreadSheet
 * This page display on the left a menu and on the right the content
 * of the page
 * 
 * @author Xavier VILAIN
 * @package jobs
 */
use Hive\Mission;
use Hive\Mission\Customizing;
use Fox\Html\Wrapper;
use Fox\Core\Errors;
use Fox\Generator;
use Fox\Html\Navigation\Menu\Item;
use Fox\Html\Navigation\Menu\Item\Sub;
use Fox\Html\Navigation\Menu;

/* $mission Hive\Mission */
$mission = Xview::instance()->component('missions', 'object');
if(!$mission->exists())
{
	Xview::instance()->systemError(null,Generator::url('missions','search'));
	exit;
}
$id = $mission->getId();
$resData = $mission->getData();
// Parse Data to save Job Information
$resData['kn_number_year_left'] = $resData['kn_number_year_min'];
$resData['kn_number_year_right'] = $resData['kn_number_year_max'];
$resData['salary_left'] = $resData['salary_low'] ;
$resData['salary_right'] = $resData['salary_high'];
$taskNb = count($mission->tasks());
$partnersNb = $mission->countCandidates();
$announces = $mission->announces();
$announcesNb = count($announces);
$documentsNb =  $mission->countDocuments();
$messagesNb =  $mission::countMessages($mission->missionid);
$defaultPage = 'process';
// Main Config
$page = Xview::instance()->val('page','process');
$customizing = Hive\Mission\Customizing::instance();
$master = Mission::BUNDLE_NAME;
$action = 'main';

$wrapper = new Wrapper('leftCol');
$favMenu = new Menu('leftMenu',Msg(Hive::BUNDLE,Hive::BOOKMARKS));
$wrapper->add($favMenu);

$candidatsMenu = new Menu('candidatsMenu',Msg(Hive::BUNDLE,Hive::CONTACT_CANDIDATES));
$wrapper->add($candidatsMenu);
// News Feed
/*
$favMenu->add(new Item('newsfeed',Msg(Hive::BUNDLE,Hive::MENU_NEW_FEEDS),
						Generator::url($master,'newsfeed',null,
							array('mid' => $id))),'');
*/
// Contacts / Partners
$favMenu->add(new Item('partners',Msg(Hive::BUNDLE,Hive::MENU_CONTACTS),
						Generator::url($master,'partners',null,
							array('mid' => $id)),null,'contacts'));
					
// Mission Detail
$favMenu->add(new Item('detail',Msg(Hive::BUNDLE,Hive::MENU_INFO_DETAIL),
						Generator::url($master,'detail',null,array('mid' => $id)),
						null,'personnal'));
// Announces						
$favMenu->add(new Item('announces',Msg(Hive::BUNDLE,Hive::TITLE_ANNOUNCES),
						Generator::url($master,'announces',null,array('mid' => $id)),
						$announcesNb));
// Documents						
$favMenu->add(new Item('documents',Msg(Hive::BUNDLE,Hive::MENU_DOCUMENTS),
						Generator::url($master,'documents',null,
							array('mid' => $id)),
					$documentsNb));
						
// Tasks
$favMenu->add(new Item('tasks',Msg(Hive::BUNDLE,Hive::TITLE_TASKS),
						Generator::url($master,'tasks',null,
							array('mid' => $id)),
					$taskNb));
// Search Candidates
$candidatsMenu->add(new Item('matches',
						Msg(Hive::BUNDLE,Hive::LABEL_SEARCH),
						Generator::url($master,'matches',null,
							array('mid' => $id)),
						$partnersNb,'candidats'));
// Process Candidates
$procRoot = new Item('process',
						Msg(Hive::BUNDLE,Hive::MENU_PROCESS),
						Generator::url($master,'process',null,
							array('mid' => $id)));
// Sub Menu
$stepCounter = $mission->countBySteps();
foreach($mission->workflow()->steps() as $step) {
	if(isset($stepCounter[$step->stepid])) {
		$nb = $stepCounter[$step->stepid][0]['nb'];
	} else {
		$nb = 0;
	}
	$procRoot->add(new Item('process_'.$step->stepid,
							$step->label(),
							Generator::url($master,'process','step',
								array('mid' => $id,'step'=>$step->stepid)),$nb));
}
$candidatsMenu->add($procRoot);

// Candidates Messages
$candidatsMenu->add(new Item('messages',
						Msg(Hive::BUNDLE,Hive::MESSAGES),
						Generator::url($master,'messages',null,
							array('mid' => $id)),
							$messagesNb));						

if(isset($_GET['uri']) && !empty($_GET['uri']))
{
	$parts = explode('-', $_GET['uri']);
	if(count($parts) > 2)
	{
		$url = Generator::url($parts[0],$parts[1],$parts[2]);
	} else {
		$url = Generator::url($parts[0],$parts[1]);
	}
	if(!Xview::instance()->isModule($url)) {
		$url = null;
	}
}
if(empty($url))
{
	$params = array('mid' => $id,'return' => true);
	list($url,$page) = $customizing->defaultSummaryPage($params);
}
$item = $wrapper->item($page);
if(!empty($item)) {
	$wrapper->item($page)->selected();
}
$edit = '<div id="leftColContainer">
			<div id="leftCol">
			<div id="pagelet_welcome_box">
				<div class="hive-missions-box">
				</div>
			</div>
			<div id="pagelet_navigation">
				<div class="uiFutureSideNav">
				'.$wrapper->render().'
			</div>
		</div>
		</div>';
?>
<div id="leftColContainer" >
	<div id="leftCol">
		<div id="pagelet_welcome_box">
			<div class="hive-contact-box">
				<div class="clearfix uiImageBlock fbxWelcomeBox">
					<a href="?mod=missions&act=main&page=summary&uri=missions-summary&mid=<?php echo $id;?>" class="uiImageBlockMediumImage lfloat slink">
						
						<img class="fbxWelcomeBoxImg  img" src="<?php 
						$imageSrc = $mission->company()->logo();
						if(empty($imageSrc))
						{
							$imageSrc = _URL_."/images/companies/void.gif";
						}
						echo $imageSrc;
						?>" />
					</a>
					<div class="uiImageBlockContent" >
						<span class="title"><?php echo $mission->companyName(); ?></span>
						<div class="i"><?php echo $mission->title; ?></div>
					</div>
				</div>			
			</div>
		</div>
		<div id="pagelet_navigation">
			<div class="uiFutureSideNav">
			<?php echo $wrapper->render(); ?>
			</div>
		</div>
	</div>
</div>
<div id="contentCol" class="hasLeftCol">
	<?php echo Xview::instance()->display($url); ?>
</div>